package com.tanhua.api.impl.db;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.api.db.SoulQuestionListApi;
import com.tanhua.domain.db.SoulQuestion;
import com.tanhua.domain.db.SoulQuestionList;
import com.tanhua.domain.db.SoulQuestionOption;
import com.tanhua.domain.mongo.SoulLock;
import com.tanhua.domain.vo.SoulQuestionListVo;
import com.tanhua.domain.vo.SoulQuestionOptionVo;
import com.tanhua.domain.vo.SoulQuestionVo;
import com.tanhua.mapper.db.SoulQuestionListMapper;
import com.tanhua.mapper.db.SoulQuestionMapper;
import com.tanhua.mapper.db.SoulQuestionOptionMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * @author Richard Wang
 * @date 2021/9/5 9:47
 */
@Service
public class SoulQuestionListApiImpl implements SoulQuestionListApi {

    @Autowired
    private SoulQuestionListMapper soulQuestionListMapper;

    @Autowired
    private SoulQuestionMapper soulQuestionMapper;

    @Autowired
    private SoulQuestionOptionMapper soulQuestionOptionMapper;


    @Override
    public List<SoulQuestionListVo> findSoulQuestion(SoulLock soulLock) {

        //1.全查试卷库(三种等级的试卷)
        QueryWrapper<SoulQuestionList> qr01 = new QueryWrapper<>();
        List<SoulQuestionList> list01 = soulQuestionListMapper.selectList(qr01);

        //(1).将SoulQuestionList赋值到SoulQuestionListVo,并返回List<SoulQuestionListVo>
        ArrayList<SoulQuestionListVo> voList01 = new ArrayList<>();
        for (SoulQuestionList soulQuestionList : list01) {
            SoulQuestionListVo soulQuestionListVo = new SoulQuestionListVo();
            Integer questionLevel = soulQuestionList.getId();
            //2.全查特定等级(基础,中级,高级)试卷下的试题(随机选择10条)
            QueryWrapper<SoulQuestion> qr02 = new QueryWrapper<>();
            qr02.eq("question_level", questionLevel);
            List<SoulQuestion> list02temp = soulQuestionMapper.selectList(qr02);
            //todo:随后要改参数()
            //从查询出来的试题中随机截取10条
            Set<SoulQuestion> randomEleSet = RandomUtil.randomEleSet(list02temp, 2);
            List<SoulQuestion> list02 = new ArrayList<>(randomEleSet);

            //(2)将SoulQuestion赋值到SoulQuestionVo,并返回List<SoulQuestionVo>
            ArrayList<SoulQuestionVo> voList02 = new ArrayList<>();
            for (SoulQuestion soulQuestion : list02) {
                SoulQuestionVo soulQuestionVo = new SoulQuestionVo();
                Integer questionId = soulQuestion.getId();
                //3.全查特定试题试卷下的答案选项
                QueryWrapper<SoulQuestionOption> qr03 = new QueryWrapper<>();
                qr03.eq("question_id", questionId);
                List<SoulQuestionOption> list03 = soulQuestionOptionMapper.selectList(qr03);

                //(3)将SoulQuestionOption赋值到SoulQuestionOptionVo,并返回List<SoulQuestionOptionVo>
                ArrayList<SoulQuestionOptionVo> voList03 = new ArrayList<>();
                for (SoulQuestionOption soulQuestionOption : list03) {
                    SoulQuestionOptionVo soulQuestionOptionVo = new SoulQuestionOptionVo();
                    soulQuestionOptionVo.setId(soulQuestionOption.getId().toString());
                    soulQuestionOptionVo.setOption(soulQuestionOption.getOptionContent());
                    voList03.add(soulQuestionOptionVo);
                }

                soulQuestionVo.setId(soulQuestion.getId().toString());
                soulQuestionVo.setQuestion(soulQuestion.getQuestion());
                soulQuestionVo.setOptions(voList03);
                voList02.add(soulQuestionVo);
            }

            BeanUtil.copyProperties(soulQuestionList, soulQuestionListVo);
            soulQuestionListVo.setId(soulQuestionList.getId().toString());
            soulQuestionListVo.setQuestions(voList02);
            //通过SoulLock表中该用户的不同等级试卷的解锁记录情况,设定当前登录用户在不同的试卷等级下的状态
            String level = soulQuestionListVo.getId();
            if (level.equals("1")) {
                soulQuestionListVo.setIsLock(soulLock.getBase());
            } else if (level.equals("2")) {
                soulQuestionListVo.setIsLock(soulLock.getMiddle());
            } else {
                soulQuestionListVo.setIsLock(soulLock.getHigh());
            }

            voList01.add(soulQuestionListVo);
        }
        return voList01;
    }


    /**
     * 根据 id 查询等级
     * @param id
     * @return
     */
    @Override
    public SoulQuestionList findById(Integer id) {
        return soulQuestionListMapper.selectById(id);
    }


    /**
     * 查询所有问题等级
     * @return
     */
    @Override
    public List<SoulQuestionList> findAll() {
        QueryWrapper<SoulQuestionList> queryWrapper = new QueryWrapper<>();
        return soulQuestionListMapper.selectList(queryWrapper);
    }
}
